import { updateTransferNoApi } from "@/api/waybillManagement/customerServiceWaybill";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formState, emit, loading } = state;
  const submitHandle = () => {
    const params = formState.value.map((t: any) => {
      return { transferNo: t.transferNo === "" ? undefined : t.transferNo, fastMail: t.fastMail, waybillNum: t.shipmentNumber, boxId: t.id, isMasterNum: t.isMasterNum ? 1 : 0 };
    });

    const arr: boolean[] = [];
    params.length &&
      params.forEach((t: any) => {
        if (t.isMasterNum === 1) {
          arr.push(t.isMasterNum);
        }
      });

    if (arr.length > 1) return message.error("只能设置一个主单号");
    loading.value = true;
    updateTransferNoApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };
  const cancelHandle = () => {
    emit("update:visible", false);
  };
  return {
    submitHandle,
    cancelHandle
  };
}
